package com.jh.paymentchannel.controller;

import cn.jh.common.exception.ServiceException;
import cn.jh.common.utils.CommonsUtil;
import com.jh.paymentchannel.service.freeQuick.FreeQuickWithdrawTempService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author zhugu
 * @version 1.0
 * @Date 2019/1/3 17:09
 */
@RestController
public class FreeQuickWithdrawTempController {
    private static final Logger log = LoggerFactory.getLogger(FreeQuickWithdrawTempController.class);

    private final FreeQuickWithdrawTempService freeQuickWithdrawTempService;

    public FreeQuickWithdrawTempController(FreeQuickWithdrawTempService freeQuickWithdrawTempService) {
        this.freeQuickWithdrawTempService = freeQuickWithdrawTempService;
    }

    /**
     * 分页查询无卡交易记录
     * @param requestNo 请求流水号
     * @param payStatus 交易状态
     * @param payType 交易类型
     * @param startTime 开始时间
     * @param endTime 结束时间
     */
    @PostMapping(value = "/v1.0/paymentchannel/FreeQuickWithdrawTemp/cardlessTransactionPage")
    public Object cardlessTransactionPage(@RequestParam(required = false) String requestNo,
                                          @RequestParam(required = false) String payStatus,
                                          @RequestParam(required = false) String payType,
                                          @RequestParam(required = false) String startTime,
                                          @RequestParam(required = false) String endTime,
                                          @RequestParam(value = "page", defaultValue = "0", required = false) int page,
                                          @RequestParam(value = "size", defaultValue = "20", required = false) int size,
                                          @RequestParam(value = "order", defaultValue = "DESC", required = false) Sort.Direction direction,
                                          @RequestParam(value = "sort", defaultValue = "createTime", required = false) String sortProperty) {
        Pageable pageable = new PageRequest(page, size, new Sort(direction,sortProperty));
        try {
            Map<String, Object> cardlessTransactionPage = freeQuickWithdrawTempService.cardlessTransactionPage(requestNo, payStatus, payType, startTime, endTime, pageable);
            return CommonsUtil.returnSuccess("查询成功！", cardlessTransactionPage);
        } catch (ServiceException e) {
            e.printStackTrace();
            return CommonsUtil.returnError(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return CommonsUtil.returnError("服务器异常，请稍后再试！");
        }
    }
}
